<?php
/* Smarty version 5.5.2, created on 2025-10-02 15:01:45
  from 'file:index/user_manage.tpl' */

/* @var \Smarty\Template $_smarty_tpl */
if ($_smarty_tpl->getCompiled()->isFresh($_smarty_tpl, array (
  'version' => '5.5.2',
  'unifunc' => 'content_68de2359335df3_13866753',
  'has_nocache_code' => false,
  'file_dependency' => 
  array (
    '4bcc4ae50a0fe2b74a7495f557c877ff8b2b865b' => 
    array (
      0 => 'index/user_manage.tpl',
      1 => 1759197765,
      2 => 'file',
    ),
  ),
  'includes' => 
  array (
  ),
))) {
function content_68de2359335df3_13866753 (\Smarty\Template $_smarty_tpl) {
$_smarty_current_dir = 'D:\\phpstudy_pro\\WWW\\Blog\\Admin\\View\\index';
?><!DOCTYPE html>
<base href="http://localhost:63342/Blog/" />
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>
    <style>
        /* 样式同之前的保持一致 */
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Microsoft YaHei', Arial, sans-serif; background-color: #f5f5f5; height: 100vh; overflow: hidden; }
        .user-management { background: white; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); padding: 20px; }
        .toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; padding-bottom: 15px; border-bottom: 1px solid #f0f0f0; }
        .btn { padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: all 0.3s ease; }
        .btn-primary { background: #667eea; color: white; }
        .btn-primary:hover { background: #5a6fd8; }
        .btn-danger { background: #ff4757; color: white; }
        .btn-danger:hover { background: #ff3742; }
        .search-box { padding: 8px 12px; border: 1px solid #ddd; border-radius: 4px; width: 200px; }
        .user-table { width: 100%; border-collapse: collapse; }
        .user-table th, .user-table td { padding: 12px; text-align: left; border-bottom: 1px solid #f0f0f0; }
        .user-table th { background: #f8f9fa; font-weight: bold; color: #333; }
        .status-active { background: #28a745; color: white; padding: 2px 8px; border-radius: 12px; font-size: 12px; }
        .status-disabled { background: #ff4757; color: white; padding: 2px 8px; border-radius: 12px; font-size: 12px; }
        .user-avatar { width: 40px; height: 40px; border-radius: 50%; background: #667eea; display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; }
        .btn-edit, .btn-delete, .btn-disable { padding: 4px 8px; font-size: 12px; cursor: pointer; border: none; border-radius: 4px; }
        .btn-edit { background: #667eea; color: white; }
        .btn-delete { background: #ff4757; color: white; }
        .btn-disable { background: #ffbc00; color: white; }
        
        /* 提示框样式 */
        .message-box {
            position: fixed; 
            top: 50%; 
            left: 50%; 
            transform: translate(-50%, -50%); 
            background: white; 
            padding: 20px; 
            border-radius: 8px; 
            box-shadow: 0 4px 16px rgba(0,0,0,0.2); 
            z-index: 1000; 
            min-width: 300px;
            text-align: center;
            display: none;
        }
        
        .message-box.success {
            border-left: 4px solid #28a745;
        }
        
        .message-box.error {
            border-left: 4px solid #dc3545;
        }
        
        .message-content {
            margin-bottom: 15px;
            font-size: 14px;
            color: #333;
        }
        
        .message-actions {
            display: flex;
            justify-content: center;
        }
        
        .message-btn {
            padding: 6px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            background: #667eea;
            color: white;
        }
        
        /* 新建用户模态框样式 */
        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 999;
            display: none;
        }
        
        .modal-content {
            background: white;
            border-radius: 8px;
            padding: 20px;
            width: 90%;
            max-width: 500px;
            max-height: 80vh;
            overflow-y: auto;
        }
        
        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .modal-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }
        
        .modal-close {
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
            color: #666;
        }
        
        .form-group {
            margin-bottom: 15px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
            color: #555;
        }
        
        .form-group input {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        
        .form-group select {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        
        .form-actions {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="user-management">
        <h2>用户管理</h2>

        <!-- 工具栏：按钮和搜索框 -->
        <div class="toolbar">
            <div>
                <button class="btn btn-primary" onclick="openCreateUserModal()">+ 新建用户</button>
                <button class="btn btn-danger" onclick="batchDelete()">批量删除</button>
            </div>
            <div>
                <input type="text" id="searchInput" class="search-box" placeholder="搜索用户...">
                <button class="btn btn-primary" onclick="searchUser()">搜索</button>
            </div>
        </div>

        <!-- 用户表格 -->
        <table class="user-table">
            <thead>
                <tr>
                    <th><input type="checkbox" id="selectAll"></th>
                    <th>头像</th>
                    <th>用户名</th>
                    <th>邮箱</th>
                    <th>注册时间</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="user-table-body">
                <?php
$_from = $_smarty_tpl->getSmarty()->getRuntime('Foreach')->init($_smarty_tpl, $_smarty_tpl->getValue('users'), 'user');
$foreach0DoElse = true;
foreach ($_from ?? [] as $_smarty_tpl->getVariable('user')->value) {
$foreach0DoElse = false;
?>
                    <tr>
                        <td><input type="checkbox" class="user-checkbox" value="<?php echo $_smarty_tpl->getValue('user')['id'];?>
"></td>
                        <td>
                            <div class="user-avatar"><?php echo mb_strtoupper((string) substr((string) $_smarty_tpl->getValue('user')['username'], (int) 0, (int) 1) ?? '', 'UTF-8');?>
</div>
                        </td>
                        <td><?php echo $_smarty_tpl->getValue('user')['username'];?>
</td>
                        <td><?php echo $_smarty_tpl->getValue('user')['email'];?>
</td>
                        <td><?php echo $_smarty_tpl->getValue('user')['created_time'];?>
</td>
                        <td>

                            <?php $_smarty_tpl->assign('statusClass', "disabled", false, NULL);?>
                            <?php $_smarty_tpl->assign('statusText', "禁用", false, NULL);?>
                            <?php if ($_smarty_tpl->getValue('user')['status'] == 1 || $_smarty_tpl->getValue('user')['status'] == 'active' || $_smarty_tpl->getValue('user')['status'] == '正常') {?>
                                <?php $_smarty_tpl->assign('statusClass', "active", false, NULL);?>
                                <?php $_smarty_tpl->assign('statusText', "正常", false, NULL);?>
                            <?php }?>
                            <span class="status-<?php echo $_smarty_tpl->getValue('statusClass');?>
"><?php echo $_smarty_tpl->getValue('statusText');?>
</span>
                        </td>
                        <td>
                            <button class="btn btn-edit" onclick="editUser('<?php echo $_smarty_tpl->getValue('user')['id'];?>
')">编辑</button>

                            <?php $_smarty_tpl->assign('actionText', "启用", false, NULL);?>
                            <?php $_smarty_tpl->assign('actionType', "active", false, NULL);?>
                            <?php if ($_smarty_tpl->getValue('user')['status'] == 1 || $_smarty_tpl->getValue('user')['status'] == 'active' || $_smarty_tpl->getValue('user')['status'] == '正常') {?>
                                <?php $_smarty_tpl->assign('actionText', "禁用", false, NULL);?>
                                <?php $_smarty_tpl->assign('actionType', "disable", false, NULL);?>
                            <?php }?>
                            <button class="btn btn-disable" onclick="changeUserStatus('<?php echo $_smarty_tpl->getValue('user')['id'];?>
', '<?php echo $_smarty_tpl->getValue('actionType');?>
')"><?php echo $_smarty_tpl->getValue('actionText');?>
</button>
                            <button class="btn btn-delete" onclick="deleteUser('<?php echo $_smarty_tpl->getValue('user')['id'];?>
')">删除</button>
                        </td>
                    </tr>
                <?php
}
if ($foreach0DoElse) {
?>
                    <tr>
                        <td colspan="7" style="text-align: center; padding: 40px; color: #666;">
                            暂无用户数据
                        </td>
                    </tr>
                <?php
}
$_smarty_tpl->getSmarty()->getRuntime('Foreach')->restore($_smarty_tpl, 1);?>
            </tbody>
        </table>
    </div>

        <!-- 提示框 -->
        <div id="messageBox" class="message-box">
            <div class="message-content"></div>
            <div class="message-actions">
                <button class="message-btn" onclick="hideMessageBox()">确定</button>
            </div>
        </div>
        
        <!-- 新建用户模态框 -->
        <div id="createUserModal" class="modal-overlay">
            <div class="modal-content">
                <div class="modal-header">
                    <h3 class="modal-title">新建用户</h3>
                    <button class="modal-close" onclick="closeCreateUserModal()">&times;</button>
                </div>
                <form id="createUserForm">
                    <div class="form-group">
                        <label for="newUsername">用户名 *</label>
                        <input type="text" id="newUsername" name="username" required placeholder="请输入用户名">
                    </div>
                    <div class="form-group">
                        <label for="newEmail">邮箱 *</label>
                        <input type="email" id="newEmail" name="email" required placeholder="请输入邮箱地址">
                    </div>
                    <div class="form-group">
                        <label for="newPassword">密码（默认123456）</label>
                        <input type="password" id="newPassword" name="password" placeholder="请输入密码">
                    </div>
                    <div class="form-group">
                        <label for="newStatus">状态</label>
                        <select id="newStatus" name="status">
                            <option value="1" selected>正常</option>
                            <option value="0">禁用</option>
                        </select>
                    </div>
                    <div class="form-actions">
                        <button type="button" class="btn btn-cancel" onclick="closeCreateUserModal()">取消</button>
                        <button type="submit" class="btn btn-primary">保存</button>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 编辑用户模态框 -->
        <div id="editUserModal" class="modal-overlay">
            <div class="modal-content">
                <div class="modal-header">
                    <h3 class="modal-title">编辑用户</h3>
                    <button class="modal-close" onclick="closeEditUserModal()">&times;</button>
                </div>
                <form id="editUserForm">
                    <input type="hidden" id="editUserId" name="id">
                    <div class="form-group">
                        <label for="editUsername">用户名 *</label>
                        <input type="text" id="editUsername" name="username" required placeholder="请输入用户名">
                    </div>
                    <div class="form-group">
                        <label for="editEmail">邮箱 *</label>
                        <input type="email" id="editEmail" name="email" required placeholder="请输入邮箱地址">
                    </div>
                    <div class="form-group">
                        <label for="editPassword">密码（留空则不修改）</label>
                        <input type="password" id="editPassword" name="password" placeholder="请输入密码">
                        <div class="help-text">留空表示不修改当前密码</div>
                    </div>
                    <div class="form-group">
                        <label for="editStatus">状态</label>
                        <select id="editStatus" name="status">
                            <option value="1">正常</option>
                            <option value="0">禁用</option>
                        </select>
                    </div>
                    <div class="form-actions">
                        <button type="button" class="btn btn-cancel" onclick="closeEditUserModal()">取消</button>
                        <button type="submit" class="btn btn-primary">保存</button>
                    </div>
                </form>
            </div>
        </div>

    <?php echo '<script'; ?>
>
        // 全选/取消全选
        document.getElementById('selectAll').addEventListener('change', function() {
            const checkboxes = document.querySelectorAll('.user-checkbox');
            checkboxes.forEach(checkbox => {
                checkbox.checked = this.checked;
            });
        });
        
        // 编辑用户表单提交
        document.getElementById('editUserForm').addEventListener('submit', function(event) {
            event.preventDefault();
            
            const formData = new FormData(this);
            const params = {};
            
            // 将FormData转换为普通对象
            for (const [key, value] of formData.entries()) {
                params[key] = value;
            }
            
            const username = params.username.trim();
            const email = params.email.trim();
            const userId = params.id;
            
            if (!username || !email || !userId) {
                showMessageBox('用户名、邮箱和用户ID不能为空', false);
                return;
            }
            
            // 构建POST数据
            let postData = '';
            Object.keys(params).forEach(key => {
                if (postData) postData += '&';
                postData += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
            });
            
            ajaxRequest(
                'admin.php?c=User&a=update',
                function(response) {
                    if (response.success) {
                        showMessageBox(response.message);
                        closeEditUserModal();
                        // 刷新用户列表
                        setTimeout(function() {
                            refreshUserList();
                        }, 1000);
                    } else {
                        showMessageBox(response.message, false);
                    }
                },
                function(error) {
                    showMessageBox('请求失败：' + error, false);
                },
                postData,
                'POST'
            );
        });

        // 显示提示框
        function showMessageBox(message, isSuccess = true) {
            const messageBox = document.getElementById('messageBox');
            const messageContent = messageBox.querySelector('.message-content');
            
            messageContent.textContent = message;
            
            // 设置提示框样式
            if (isSuccess) {
                messageBox.classList.add('success');
                messageBox.classList.remove('error');
            } else {
                messageBox.classList.add('error');
                messageBox.classList.remove('success');
            }
            
            // 显示提示框
            messageBox.style.display = 'block';
        }
        
        // 隐藏提示框
        function hideMessageBox() {
            document.getElementById('messageBox').style.display = 'none';
        }
        
        // 打开新建用户模态框
        function openCreateUserModal() {
            document.getElementById('createUserModal').style.display = 'flex';
        }
        
        // 关闭新建用户模态框
        function closeCreateUserModal() {
            document.getElementById('createUserModal').style.display = 'none';
            // 重置表单
            document.getElementById('createUserForm').reset();
        }
        
        // 刷新用户列表
        function refreshUserList() {
            // 这里可以选择重新加载整个页面，或者通过AJAX请求获取最新的用户数据并更新表格
            // 为了简单起见，这里选择重新加载整个页面
            window.location.reload();
        }
        
        // 简化的AJAX请求函数，支持GET和POST方法
        function ajaxRequest(url, successCallback, errorCallback, data = null, method = 'GET') {
            const xhr = new XMLHttpRequest();
            xhr.open(method, url, true);
            xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
            
            if (method === 'POST') {
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            }
            
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        try {
                            const response = JSON.parse(xhr.responseText);
                            if (successCallback) {
                                successCallback(response);
                            }
                        } catch (e) {
                            if (errorCallback) {
                                errorCallback('解析响应失败');
                                console.error('JSON解析错误:', e);
                            }
                        }
                    } else {
                        if (errorCallback) {
                            errorCallback('请求失败，状态码：' + xhr.status);
                        }
                    }
                }
            };
            
            // 发送数据
            if (method === 'POST' && data) {
                xhr.send(data);
            } else {
                xhr.send();
            }
        }
        
        // 关闭编辑用户模态框
        function closeEditUserModal() {
            document.getElementById('editUserModal').style.display = 'none';
            // 重置表单
            document.getElementById('editUserForm').reset();
        }
        
        // 编辑用户 - 打开模态框并加载用户数据
        function editUser(userId) {
            // 首先通过AJAX获取用户数据
            const url = 'admin.php?c=User&a=getUser&id=' + userId;
            ajaxRequest(
                url,
                function(response) {
                    if (response.success && response.user) {
                        // 填充表单数据
                        const user = response.user;
                        document.getElementById('editUserId').value = user.id;
                        document.getElementById('editUsername').value = user.username;
                        document.getElementById('editEmail').value = user.email;
                        document.getElementById('editStatus').value = user.status == 1 || user.status == 'active' || user.status == '正常' ? '1' : '0';
                        
                        // 显示编辑用户模态框
                        document.getElementById('editUserModal').style.display = 'flex';
                    } else {
                        showMessageBox('获取用户数据失败', false);
                    }
                },
                function(error) {
                    showMessageBox('请求失败：' + error, false);
                }
            );
        }

        // 更改用户状态
        function changeUserStatus(userId, action) {
            const statusText = action === 'disable' ? '禁用' : '启用';
            if (confirm('确定要' + statusText + '该用户吗？')) {
                // 使用完整的绝对路径URL
                const url = 'admin.php?c=User&a=changeStatus&id=' + userId + '&status=' + action;
                ajaxRequest(
                    url,
                    function(response) {
                        if (response.success) {
                            showMessageBox(response.message);
                            // 延迟刷新列表，让用户看到提示信息
                            setTimeout(refreshUserList, 1500);
                        } else {
                            showMessageBox(response.message, false);
                        }
                    },
                    function(error) {
                        showMessageBox(error, false);
                    }
                );
            }
        }

        // 删除用户
        function deleteUser(userId) {
            if (confirm('确定要删除该用户吗？')) {
                // 使用完整的绝对路径URL
                const url = 'admin.php?c=User&a=deleteUser&id=' + userId;
                ajaxRequest(
                    url,
                    function(response) {
                        if (response.success) {
                            showMessageBox(response.message);
                            // 延迟刷新列表，让用户看到提示信息
                            setTimeout(refreshUserList, 1500);
                        } else {
                            showMessageBox(response.message, false);
                        }
                    },
                    function(error) {
                        showMessageBox(error, false);
                    }
                );
            }
        }

        // 批量删除
        function batchDelete() {
            const selectedUsers = [];
            const checkboxes = document.querySelectorAll('.user-checkbox:checked');
            
            if (checkboxes.length === 0) {
                showMessageBox('请至少选择一个用户', false);
                return;
            }
            
            checkboxes.forEach(checkbox => {
                selectedUsers.push(checkbox.value);
            });
            
            if (confirm('确定要删除选中的 ' + checkboxes.length + ' 个用户吗？')) {
                // 使用完整的绝对路径URL
                const url = 'admin.php?c=User&a=batchDelete&ids=' + selectedUsers.join(',');
                ajaxRequest(
                    url,
                    function(response) {
                        if (response.success) {
                            showMessageBox(response.message);
                            // 延迟刷新列表，让用户看到提示信息
                            setTimeout(refreshUserList, 1500);
                        } else {
                            showMessageBox(response.message, false);
                        }
                    },
                    function(error) {
                        showMessageBox(error, false);
                    }
                );
            }
        }

        // 搜索用户
        function searchUser() {
            const keyword = document.getElementById('searchInput').value.trim();
            if (keyword) {
                // 使用完整的绝对路径URL
                const url = 'admin.php?c=User&a=search&keyword=' + encodeURIComponent(keyword);
                ajaxRequest(
                    url,
                    function(response) {
                        if (response.success) {
                            // 这里可以选择更新表格内容，或者跳转到搜索结果页面
                            // 由于搜索结果可能很多，我们选择跳转到搜索结果页面
                            window.location.href = 'admin.php?c=User&a=search&keyword=' + encodeURIComponent(keyword);
                        } else {
                            showMessageBox(response.message, false);
                        }
                    },
                    function(error) {
                        showMessageBox(error, false);
                    }
                );
            } else {
                window.location.href = 'admin.php?c=User&a=user_manage';
            }
        }

        // 支持回车键搜索
        document.getElementById('searchInput').addEventListener('keyup', function(event) {
            if (event.key === 'Enter') {
                searchUser();
            }
        });
        
        // 提交新建用户表单
        document.getElementById('createUserForm').addEventListener('submit', function(event) {
            event.preventDefault();
            
            const username = document.getElementById('newUsername').value.trim();
            const email = document.getElementById('newEmail').value.trim();
            const password = document.getElementById('newPassword').value;
            const status = document.getElementById('newStatus').value;
            
            if (!username || !email) {
                showMessageBox('用户名和邮箱不能为空', false);
                return;
            }
            
            // 使用POST方法，将数据作为请求体发送
            const url = 'admin.php?c=User&a=update';
            const data = 'username=' + encodeURIComponent(username) + '&email=' + encodeURIComponent(email) + '&password=' + encodeURIComponent(password) + '&status=' + encodeURIComponent(status);
            ajaxRequest(
                url,
                function(response) {
                    if (response.success) {
                        showMessageBox(response.message);
                        closeCreateUserModal();
                        // 延迟刷新列表，让用户看到提示信息
                        setTimeout(refreshUserList, 1500);
                    } else {
                        showMessageBox(response.message, false);
                    }
                },
                function(error) {
                    showMessageBox(error, false);
                },
                data,
                'POST'
            );
        });
    <?php echo '</script'; ?>
>
</body>
</html>
<?php }
}
